﻿using System;
using System.Drawing;
using System.Windows.Forms;
using AiFrame.InterfaceLib.Windows.Controls;

namespace AiFrame.Windows.Controls.DataNavigationBar
{
    public enum DataNavBarButtonState
    {
        Normal,
        Hovered
    }

    /// <summary>
    /// Stellt eine Schaltfläche in der DataNavigationBar dar.
    /// </summary>
    class DataNavigationBarButton: IDataNavigationBarButton
    {
        private PictureBox _pictureBox;
        private string _caption;
        private Image _normalImage;
        private Image _hoveredImage;
        private Label _captionLabel;
        private IDataNavigationBarTheme _theme;
        private OnDataNavBarClickProcedure _clickMethod;
        private DataNavBarButtonState _state = DataNavBarButtonState.Normal;
        private Control _parent;
        private object _saveObject;
        private int _top;
        private int _left;
        private int _width;
 
        private const int IMAGE_LABEL_DIFF = 2;/// Abstand zwischen der PictureBox und dem Label

        public DataNavigationBarButton(string name, string caption, 
            Image normalImage, Image hoveredImage, OnDataNavBarClickProcedure proc,
            IDataNavigationBarTheme theme, object saveObject)
        {
            _caption = caption;
            _normalImage = normalImage;
            _hoveredImage = hoveredImage;
            _theme = theme;
            _saveObject = saveObject;
            _clickMethod = proc;

            Initialize();
        }

        public DataNavigationBarButton(string name, string caption, 
            Image normalImage, Image hoveredImage, OnDataNavBarClickProcedure proc,
            IDataNavigationBarTheme theme)
        {
            _caption = caption;
            _normalImage = normalImage;
            _hoveredImage = hoveredImage;
            _clickMethod = proc;
            _theme = theme;

            Initialize();
        }

        private void Initialize()
        {
            /* PictureBox initialisieren */
            _pictureBox = new PictureBox();
            _pictureBox.BackColor = Color.Transparent;
            _pictureBox.AutoSize = true;
            _pictureBox.Width = _hoveredImage.Width;
            _pictureBox.Height = _hoveredImage.Height;
            _pictureBox.MouseEnter += new EventHandler(_pictureBox_MouseEnter);
            _pictureBox.MouseLeave += new EventHandler(_pictureBox_MouseLeave);
            _pictureBox.MouseClick += new MouseEventHandler(_pictureBox_MouseClick);
            /* Label initialisieren */
            _captionLabel = new Label();
            _captionLabel.Text = _caption;
            _captionLabel.AutoSize = true;
            _captionLabel.BackColor = Color.Transparent;
            _captionLabel.Font = _theme.GetDataNavBarButtonFont();
            _captionLabel.ForeColor = _theme.GetDataNavBarButtonFontColor();

            State = DataNavBarButtonState.Normal;
        }

        void _pictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            /* Die OnClick-Methode aufrufen, als Sender diese Klasse übergeben,
             * damit der Empfänger daraus ein IDataNavigationBarButton casten kann */
            _clickMethod(this, e);
        }

        void _pictureBox_MouseLeave(object sender, EventArgs e)
        {
            State = DataNavBarButtonState.Normal;
        }

        void _pictureBox_MouseEnter(object sender, EventArgs e)
        {
            State = DataNavBarButtonState.Hovered;
        }

        public int Height
        {
            get
            {
                return 65;
            }
        }

        public Control Parent
        {
            get
            {
                return _parent;
            }
            set
            {
                _parent = value;
                _pictureBox.Parent = value;
                _captionLabel.Parent = value;
            }
        }

        public int Left
        {
            get
            {
                return _left;
            }
            set
            {
                _left = value;
            }
        }

        public int Top
        {
            get
            {
                return _top;
            }
            set
            {
                _top = value;
                _pictureBox.Top = value;
                _captionLabel.Top = _pictureBox.Top + _pictureBox.Height + IMAGE_LABEL_DIFF;
            }
        }

        /// <summary>
        /// Die Steuerelemente richten sich an der Breit der NavBar aus.
        /// </summary>
        public int NavBarWidth
        {
            get
            {
                return _width;
            }
            set
            {
                _width = value;
                /* Das Label soll zentriert unter der PictureBox erscheinen */
                _pictureBox.Left = value/2 - (_pictureBox.Width/2);
                _captionLabel.Left = value/2 - (_captionLabel.Width/2);
            }
        }

        public DataNavBarButtonState State
        {
            get
            {
                return _state;
            }
            set
            {
                _state = value;
                Image curImage = null;

                switch (value)
                {
                    case DataNavBarButtonState.Normal:
                        curImage = _normalImage;
                        break;
                    case DataNavBarButtonState.Hovered:
                        curImage = _hoveredImage;
                        break;
                }
                _pictureBox.Image = curImage;
            }
        }

        public object GetSaveObject()
        {
            return _saveObject;
        }
    }
}
